<template>
  <div class="app-container" id="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="模板名称" prop="templateName">
        <el-select v-model="queryParams.templateName"  placeholder="请选择模版" @change="handleQuery">
          <el-option
            v-for="template in templateList"
            :key="template.templateName"
            :label="template.templateName"
            :value="template.templateName"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="分类" prop="category">
        <el-select v-model="queryParams.category"  placeholder="请选择分类" @change="handleQuery">
        <el-option
          v-for="dict in dict.type.collegeco_eval_template_category"
          :key="dict.value"
          :label="dict.label"
          :value="dict.value"
        />
        </el-select>
      </el-form-item>
      <el-form-item label="适用对象" prop="applicableFor">
        <el-select v-model="queryParams.applicableFor" placeholder="请选择对象" @change="handleQuery">
        <el-option
          v-for="dict in dict.type.collegeco_eval_template_applicable_for"
          :key="dict.value"
          :label="dict.label"
          :value="dict.value"
        />
        </el-select>
      </el-form-item>

<!--      <el-form-item label="是否公开" prop="isPublic">-->
<!--        <el-select v-model="form.isPublic"  placeholder="请选择是否公开">-->
<!--        <el-option-->
<!--          v-for="dict in dict.type.collegeco_eval_template_is_public"-->
<!--          :key="dict.value"-->
<!--          :label="dict.label"-->
<!--          :value="dict.value"-->
<!--        />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="创建人部门ID" prop="createDept">-->
<!--        <el-input-->
<!--          v-model="queryParams.createDept"-->
<!--          placeholder="请输入创建人部门ID"-->
<!--          filterable clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <!-- 更多查询条件-->
      <template v-if="isShowMore">
        <el-form-item label="版本号" prop="version">
          <el-input
            v-model="queryParams.version"
            placeholder="请输入版本号"
            filterable clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>


      </template>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button :icon="isShowMore?'el-icon-d-arrow-right':'el-icon-d-arrow-left'" size="mini" type="warning" @click="isShowMore = !isShowMore">更多查询</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['collegeco:internTemplate:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['collegeco:internTemplate:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['collegeco:internTemplate:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['collegeco:internTemplate:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="internTemplateList" @selection-change="handleSelectionChange" stripe :height="tableHeight">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" width="55" label="序号" align="center" fixed/>
<!--      <el-table-column label="模板ID" align="center" prop="templateId" />-->
      <el-table-column label="模板名称" align="center" prop="templateName" min-width="200"/>
      <el-table-column label="分类" align="center" prop="category" width="180">
      <template slot-scope="scope">
        <dict-tag :options="dict.type.collegeco_eval_template_category" :value="scope.row.category"/>
      </template>
      </el-table-column>
      <el-table-column label="适用对象" align="center" prop="applicableFor" width="180">
        <template slot-scope="scope">
        <dict-tag :options="dict.type.collegeco_eval_template_applicable_for" :value="scope.row.applicableFor"/>
      </template>
      </el-table-column>
      <el-table-column label="模板说明或使用场景" align="center" prop="description" width="180"/>
<!--      <el-table-column label="状态" align="center" prop="status" >-->
<!--        <template slot-scope="scope">-->
<!--        <dict-tag :options="dict.type.collegeco_eval_template_status" :value="scope.row.status"/>-->
<!--      </template>-->
<!--      </el-table-column>-->
      <el-table-column label="版本号" align="center" prop="version" />
      <el-table-column label="是否公开" align="center" prop="isPublic" width="180">
        <template slot-scope="scope">
        <dict-tag :options="dict.type.collegeco_eval_template_is_public" :value="scope.row.isPublic"/>
      </template>
      </el-table-column>
      <el-table-column label="创建人部门ID" align="center" prop="createDept" width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180" fixed="right">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-info"
              @click="handleInfo(scope.row)"
              v-hasPermi="['collegeco:internTemplate:query']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['collegeco:internTemplate:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['collegeco:internTemplate:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改实习评分标准模板库（主）对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="68%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
      <el-col :span="8">
            <el-form-item label="模板名称" prop="templateName">
              <el-input v-model="form.templateName"  placeholder="请输入模板名称" />
            </el-form-item>
      </el-col>
      <el-col :span="8">
            <el-form-item label="分类" prop="category">
              <el-select v-model="form.category"  placeholder="请选择分类">
              <el-option
                v-for="dict in dict.type.collegeco_eval_template_category"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
              </el-select>
            </el-form-item>
      </el-col>
      <el-col :span="8">
            <el-form-item label="适用对象" prop="applicableFor">
              <el-select v-model="form.applicableFor"  placeholder="请选择适用对象">
              <el-option
                v-for="dict in dict.type.collegeco_eval_template_applicable_for"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
              </el-select>
            </el-form-item>
      </el-col>
          <el-col :span="24">
                <el-form-item label="模板说明或使用场景" prop="description">
                  <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
                </el-form-item>
          </el-col>
      <el-col :span="8">
            <el-form-item label="版本号" prop="version">
              <el-input v-model="form.version"  placeholder="请输入版本号" />
            </el-form-item>
      </el-col>
      <el-col :span="8">
            <el-form-item label="是否公开" prop="isPublic">
              <el-select v-model="form.isPublic"  placeholder="请选择是否公开">
              <el-option
                v-for="dict in dict.type.collegeco_eval_template_is_public"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
              </el-select>
            </el-form-item>
      </el-col>
      <el-col :span="8">
            <el-form-item label="创建人部门ID" prop="createDept">
              <el-input v-model="form.createDept"  placeholder="请输入创建人部门ID" />
            </el-form-item>
      </el-col>
        </el-row>
        <el-divider content-position="center">评分模板-评分项明细信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddHrmEvalTemplateItem">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteHrmEvalTemplateItem">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="hrmEvalTemplateItemList" :row-class-name="rowHrmEvalTemplateItemIndex" @selection-change="handleHrmEvalTemplateItemSelectionChange" ref="hrmEvalTemplateItem" :height="tableHeight" >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="评分项名称，如：工作态度、专业技能、出勤率" prop="itemName" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.itemName" placeholder="请输入评分项名称，如：工作态度、专业技能、出勤率" />
            </template>
          </el-table-column>
          <el-table-column label="该项满分" prop="maxScore" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.maxScore" placeholder="请输入该项满分" />
            </template>
          </el-table-column>
          <el-table-column label="权重" prop="weight" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.weight" placeholder="请输入权重" />
            </template>
          </el-table-column>
          <el-table-column label="评分说明，如：积极主动，按时完成任务" prop="description" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.description" placeholder="请输入评分说明，如：积极主动，按时完成任务" />
            </template>
          </el-table-column>
          <el-table-column label="排序" prop="sortOrder" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.sortOrder" placeholder="请输入排序" />
            </template>
          </el-table-column>
        </el-table>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 实习评分标准模板库（主）详情对话框 -->
    <el-dialog :title="title" :visible.sync="openInfo" width="68%" append-to-body>
      <el-descriptions border >
                        <el-descriptions-item label="模板名称" >{{form.templateName}}</el-descriptions-item>
                        <el-descriptions-item label="分类" >
                          <dict-tag :options="dict.type.collegeco_eval_template_category" :value="form.category"/>
                        </el-descriptions-item>
                        <el-descriptions-item label="适用对象" >
                          <dict-tag :options="dict.type.collegeco_eval_template_applicable_for" :value="form.applicableFor"/>
                        </el-descriptions-item>
                        <el-descriptions-item label="模板说明或使用场景" >{{form.description}}</el-descriptions-item>
                        <el-descriptions-item label="版本号" >{{form.version}}</el-descriptions-item>
                        <el-descriptions-item label="是否公开" >
                          <dict-tag :options="dict.type.collegeco_eval_template_is_public" :value="form.isPublic"/>
                        </el-descriptions-item>
                        <el-descriptions-item label="创建人部门ID" >{{form.createDept}}</el-descriptions-item>
      </el-descriptions>
          <el-divider content-position="center">评分模板-评分项明细信息</el-divider>
          <el-table :data="hrmEvalTemplateItemList" :row-class-name="rowHrmEvalTemplateItemIndex" @selection-change="handleHrmEvalTemplateItemSelectionChange" ref="hrmEvalTemplateItem">
<!--            <el-table-column type="selection" width="50" align="center" />-->
            <el-table-column label="序号" align="center" prop="index" width="50"/>
                    <el-table-column label="评分项名称，如：工作态度、专业技能、出勤率" prop="itemName" width="150" />
                    <el-table-column label="该项满分" prop="maxScore" width="150" />
                    <el-table-column label="权重" prop="weight" width="150" />
                    <el-table-column label="评分说明，如：积极主动，按时完成任务" prop="description" width="150" />
                    <el-table-column label="排序" prop="sortOrder" width="150" />
          </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInternTemplate, getInternTemplate, delInternTemplate, addInternTemplate, updateInternTemplate } from "@/api/collegeco/internTemplate";

export default {
  name: "InternTemplate",
  dicts: [
    'collegeco_eval_template_is_public',
    'collegeco_eval_template_category',
    'collegeco_eval_template_applicable_for',
  ],
  data() {
    return {
      // 是否显示更多
      isShowMore: false,
      // el-table 高度
      tableHeight: 650, // 用于存储 el-table 的高度
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedHrmEvalTemplateItem: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 实习评分标准模板库（主）表格数据
      internTemplateList: [],
      // 评分模板-评分项明细表格数据
      hrmEvalTemplateItemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示详情弹出层
      openInfo: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        templateName: null,
        category: null,
        applicableFor: null,
        description: null,
        status: null,
        version: null,
        isPublic: null,
        createDept: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        templateName: [
          { required: true, message: "模板名称，如：软件开发实习生评分标准不能为空", trigger: "blur" }
        ],
        createBy: [
          { required: true, message: "创建人ID不能为空", trigger: "blur" }
        ],
      },

      templateList: [],
    };
  },
  created() {
    this.getList();
    this.setTableHeight();
    window.addEventListener('resize', this.setTableHeight); // 监听窗口大小变化

    this.getTemplateList();
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.setTableHeight); // 移除事件监听
  },
  methods: {
    /** 查询实习评分标准模板库（主）列表 */
    getList() {
      this.loading = true;
      listInternTemplate(this.queryParams).then(response => {
        this.internTemplateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getTemplateList() {
      listInternTemplate(
        {
          pageNum: 1,
          pageSize: 10000,
        }).then(response => {
        this.templateList = response.rows;
        console.log("templateList",this.templateList)

      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openInfo = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        templateId: null,
        templateName: null,
        category: null,
        applicableFor: null,
        description: null,
        status: null,
        version: null,
        isPublic: null,
        createBy: null,
        createDept: null,
        createTime: null,
        updateTime: null
      };
      this.hrmEvalTemplateItemList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.templateId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加实习评分标准模板库（主）";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const templateId = row.templateId || this.ids
      getInternTemplate(templateId).then(response => {
        this.form = response.data;
        this.hrmEvalTemplateItemList = response.data.hrmEvalTemplateItemList;
        this.open = true;
        this.title = "修改实习评分标准模板库（主）";
      });
    },
    /** 详情按钮操作 */
    handleInfo(row) {
      this.reset();
      const templateId = row.templateId || this.ids
      getInternTemplate(templateId).then(response => {
        this.form = response.data;
            this.hrmEvalTemplateItemList = response.data.hrmEvalTemplateItemList;
        this.openInfo = true;
        this.title = "实习评分标准模板库（主）详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.hrmEvalTemplateItemList = this.hrmEvalTemplateItemList;
          if (this.form.templateId != null) {
            updateInternTemplate(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInternTemplate(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const templateIds = row.templateId || this.ids;
      this.$modal.confirm('是否确认删除实习评分标准模板库（主）编号为"' + templateIds + '"的数据项？').then(function() {
        return delInternTemplate(templateIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 评分模板-评分项明细序号 */
    rowHrmEvalTemplateItemIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 评分模板-评分项明细添加按钮操作 */
    handleAddHrmEvalTemplateItem() {
      let obj = {};
      obj.itemName = "";
      obj.maxScore = "";
      obj.weight = "";
      obj.description = "";
      obj.sortOrder = "";
      this.hrmEvalTemplateItemList.push(obj);
    },
    /** 评分模板-评分项明细删除按钮操作 */
    handleDeleteHrmEvalTemplateItem() {
      if (this.checkedHrmEvalTemplateItem.length == 0) {
        this.$modal.msgError("请先选择要删除的评分模板-评分项明细数据");
      } else {
        const hrmEvalTemplateItemList = this.hrmEvalTemplateItemList;
        const checkedHrmEvalTemplateItem = this.checkedHrmEvalTemplateItem;
        this.hrmEvalTemplateItemList = hrmEvalTemplateItemList.filter(function(item) {
          return checkedHrmEvalTemplateItem.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleHrmEvalTemplateItemSelectionChange(selection) {
      this.checkedHrmEvalTemplateItem = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('hrm/internTemplate/export', {
        ...this.queryParams
      }, `实习评分标准模板库（主）数据导出_${new Date().getTime()}.xlsx`)
    },
    // 设置表格高度
    setTableHeight() {
      const appContainer = document.getElementById('app-container');
      if (appContainer) {
        this.tableHeight = appContainer.clientHeight * 0.8;
      }
    }
  }
};
</script>
